
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <stdio.h>
#include "BlobLabeling.h"


int main(int argc,char** argv){

	CvCapture* capture;
	capture=cvCaptureFromCAM(0);
	IplImage* image;
	int w=0,x=0,y=0,cr=0,cb=0,br=0,h=0;


	cvNamedWindow("Window");
	cvNamedWindow("Tracking",0);
	cvResizeWindow("Tracking",640,480);


	if(capture){

		while(1){

			image=cvQueryFrame(capture);
					
			image=cvCloneImage(image); 
			cvSmooth(image,image,CV_GAUSSIAN,3,3);

			IplImage* img=cvCreateImage(cvGetSize(image),IPL_DEPTH_8U,3);
			IplImage* res=cvCreateImage(cvGetSize(image),IPL_DEPTH_8U,1);

			cvCvtColor(image,img,CV_BGR2YCrCb);
			 
			w=img->width,h=img->height;

			for(y=0;y<h;y++){

				for(x=0;x<w;x++){

					cr=(int)((unsigned char*)(img->imageData+img->widthStep*(y)))[(x)*3+1];
					cb=(int)((unsigned char*)(img->imageData+img->widthStep*(y)))[(x)*3+2];

//					if((cr>133 && cr<173) && (cb>77 && cb<127))
					if(cr>138 && cr<180)
						((unsigned char*)(res->imageData+res->widthStep*(y)))[(x)]=255;
					else
						((unsigned char*)(res->imageData+res->widthStep*(y)))[(x)]=0;
				
				}
			}
				
			cvMorphologyEx(res,res,NULL,NULL,CV_MOP_OPEN);
			cvMorphologyEx(res,res,NULL,NULL,CV_MOP_CLOSE);
			cvSmooth(res,res,CV_GAUSSIAN,3,3);

		
			CBlobLabeling blob;
			blob.SetParam(res,50);
			blob.DoLabeling();

			for(int i=0; i<blob.m_nBlobs;i++){
				CvPoint pt1=cvPoint(blob.m_recBlobs[i].x,blob.m_recBlobs[i].y);
				CvPoint pt2=cvPoint(pt1.x+blob.m_recBlobs[i].width,pt1.y+blob.m_recBlobs[i].height);
				CvScalar color=cvScalar(0,0,255);
				cvDrawRect(res,pt1,pt2,color);
			}

			cvShowImage("Tracking",res);
			cvShowImage("Window",image);
			
					
			cvReleaseImage(&img);
			cvReleaseImage(&res);
			cvReleaseImage(&image);

			int c=cvWaitKey(10);
			if((char)c==27)break;

					
		}
	}
	cvReleaseCapture(&capture);
	cvDestroyAllWindows();
	return 0;

}































/*
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <stdio.h>


int main(int argc,char** argv){

	CvCapture* capture;
	capture=cvCaptureFromCAM(0);
	IplImage* image;
	int w=0,x=0,y=0,cr=0,cb=0,br=0,h=0;


	cvNamedWindow("Window");
	cvNamedWindow("Tracking",0);
	cvResizeWindow("Tracking",640,480);


	if(capture){

		while(1){

			image=cvQueryFrame(capture);
					
			image=cvCloneImage(image); 
			cvSmooth(image,image,CV_GAUSSIAN,3,3);

			IplImage* img=cvCreateImage(cvGetSize(image),IPL_DEPTH_8U,3);
			IplImage* res=cvCreateImage(cvGetSize(image),IPL_DEPTH_8U,1);

			cvCvtColor(image,img,CV_BGR2YCrCb);
			 
			w=img->width,h=img->height;

			for(y=0;y<h;y++){

				for(x=0;x<w;x++){

					cr=(int)((unsigned char*)(img->imageData+img->widthStep*(y)))[(x)*3+1];
					cb=(int)((unsigned char*)(img->imageData+img->widthStep*(y)))[(x)*3+2];

//					if((cr>133 && cr<173) && (cb>77 && cb<127))
					if(cr>138 && cr<180)
						((unsigned char*)(res->imageData+res->widthStep*(y)))[(x)]=255;
					else
						((unsigned char*)(res->imageData+res->widthStep*(y)))[(x)]=0;
				
				}
			}
				
	

			cvMorphologyEx(res,res,NULL,NULL,CV_MOP_OPEN);
			cvMorphologyEx(res,res,NULL,NULL,CV_MOP_CLOSE);
			cvSmooth(res,res,CV_GAUSSIAN,3,3);


			cvShowImage("Window",image);
			cvShowImage("Tracking",res);
			
			cvReleaseImage(&img);
			cvReleaseImage(&res);
			cvReleaseImage(&image);

			int c=cvWaitKey(10);
			if((char)c==27)break;

					
		}
	}
	cvReleaseCapture(&capture);
	cvDestroyAllWindows();
	return 0;

}
*/